/*DONE* Table [dbo].[<CompanyName>$Retail Product Group$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 4.8.2020 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Retail Product Group$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Retail Product Group$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[Item Category Code] [nvarchar](20) NOT NULL,
	[Code] [nvarchar](20) NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[Shelf Label Description] [nvarchar](30) NOT NULL,
	[Item Template Code] [nvarchar](10) NOT NULL,
	[POS Menu Link] [nvarchar](20) NOT NULL,
	[Min Loc_ Prof_ Inventory] [int] NOT NULL,
	[Barcode Mask] [nvarchar](22) NOT NULL,
	[Use EAN Standard Barc_] [tinyint] NOT NULL,
	[Default Profit _] [decimal](38, 20) NOT NULL,
	[Dispense Printer Group] [nvarchar](10) NOT NULL,
	[Disable Dispense Printing] [tinyint] NOT NULL,
	[POS Inventory Lookup] [tinyint] NOT NULL,
	[Last Date Modified] [datetime] NOT NULL,
	[Variant Framework Code] [nvarchar](20) NOT NULL,
	[Primary Key] [nvarchar](50) NOT NULL,
	[Division Code] [nvarchar](10) NOT NULL,
	[Item Error Check Code] [nvarchar](10) NOT NULL,
	[Suggested Qty_ on POS] [decimal](38, 20) NOT NULL,
	[Default Base UOM] [nvarchar](10) NOT NULL,
	[Qty not in Decimal] [tinyint] NOT NULL,
	[Profit Goal _] [decimal](38, 20) NOT NULL,
	[Buyer Group Code] [nvarchar](10) NOT NULL,
	[Replen_ Data Profile] [nvarchar](20) NOT NULL,
	[Buyer ID] [nvarchar](50) NOT NULL,
	[Replen_ Transfer Rule Code] [nvarchar](10) NOT NULL,
	[Def_ Item Distr_ Type] [int] NOT NULL,
	[Def_ Item Distr_ Code] [nvarchar](20) NOT NULL,
	[Not Discountable] [tinyint] NOT NULL,
	[Phys Invt Counting Period Code] [nvarchar](10) NOT NULL,
	[Allocation Rule Code] [nvarchar](20) NOT NULL,
	[Outbound Code] [nvarchar](30) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Retail Product Group$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Item Category Code] ASC,
	[Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Retail Product Group$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END